import { _decorator, Component, Node, Sprite, SpriteFrame, AudioSourceComponent, loader, AudioClip, assetManager  } from 'cc';
import { LocalCache } from '../../utils/LocalCache';
import BaseView from '../BaseView';
import GDef from '../../config/GDef';
import GD from '../../config/GD';
import Tool from '../../tool/Tool'
import TyqEventMgr from '../../tyq3.x/tyq-event-mgr';
const { ccclass, property } = _decorator;

@ccclass('AudioMgr')
export class AudioMgr extends BaseView {
    /* class member could be defined like this */
    // dummy = '';

    /* use `property` decorator if your want the member to be serializable */
    // @property
    // serializableDummy = 0;
    @property({
        type: SpriteFrame,
        tooltip: "静音图标"
    })
    mute: SpriteFrame = null;

    @property({
        type: SpriteFrame,
        tooltip: "开启声音图标"
    })
    voice: SpriteFrame = null;

    @property({
        type: Sprite,
        tooltip: "音频设置按钮图标"
    })
    spr: Sprite = null;
    @property({
        type: Node,
        tooltip: "音频设置按钮"
    })
    Audioseting: Node = null;


    // private audio: boolean = true;//声音状态
    // private _filePath: String = 'AudioClip';
    // private soundResouces: AudioClip[] = [];//音频剪辑
    onLoad() {
        let Audio = LocalCache.getItem("Audio")
        if (!Audio) {
            LocalCache.setItem("Audio", 1);
            GD.audioEnable = true;
        } else {
            if (Audio == 1) {
                GD.audioEnable = false;
                this.btn_audioSeting_cb();
            } else {
                GD.audioEnable = true;
                this.btn_audioSeting_cb();
            }
        }
        this.bindButton(this.Audioseting, this.onClickAudio);

    }
    public onClickAudio() {
        this.btn_audioSeting_cb();


        let type = GD.audioEnable ? '开启' : '关闭';
        TyqEventMgr.ins.sendEvent('点击声音按钮:' + type);
    }

    start() {

        // Your initialization goes here.
    }

    /**
      * 声音设置
      */
    private btn_audioSeting_cb() {
        if (GD.audioEnable) {
            GD.audioEnable = false;
            LocalCache.setItem("Audio", 0);
        } else {
            LocalCache.setItem("Audio", 1);
            GD.audioEnable = true;
        }
        // this.pause();//播放或暂停声音
        this.spr.spriteFrame = GD.audioEnable ? this.voice : this.mute;
    }

    /**
   * @description 暂停声音
   */
    // public pause() {
    //     if (!this.audio) {
    //         this.audioMgr.pause();
    //     } else {
    //         this.audioMgr.play();
    //     }
    // }
}
